package com.dao.impl;

import com.dao.BaseDao;
import com.dao.intf.UserDao;
import com.entity.Relation;
import com.entity.User;

import java.util.List;

public class UserDaoImpl extends BaseDao implements UserDao {


    @Override
    public List<User> queryUserByRoleAdmin() {
        String sql="  SELECT * FROM `user` WHERE `role`=\"admin\"";

        return queryForList(User.class,sql);
//        return (List<Integer>) queryForSingleValue(sql);
    }

    @Override
    public User queryUserByUsername(String username) {
        String sql="select * from user where `username`=?";

        return queryForOne(User.class,sql,username);
    }

    @Override
    public int saveUser(User user) {
        String sql="INSERT INTO `user`(`username`,`password`,`role`,`isFreezed`) VALUES (?,?,?,?)";
        return update(sql,user.getUsername(),user.getPassword(),user.getRole(),user.getIsFreezed());
    }

    @Override
    public User queryUserByUsernameAndPassword(String username, String password) {
        String sql="select * from `user` where `username`=? and `password`=? ";

        return queryForOne(User.class,sql,username,password);

    }

    @Override
    public List<Relation> queryFollowerName(String username) {

        String sql="SELECT `followerName` followerName  FROM `relation` WHERE  `followedName`=?";
        return queryForList(Relation.class,sql,username);
    }

    @Override
    public Integer queryFollowerNum(String username) {
        String sql="SELECT count(*) FROM `relation` WHERE  `followedName`=?";


        Number count = (Number) queryForSingleValue(sql,username);
        return count.intValue();
    }

    @Override
    public List<Relation> queryFollowedName(String username) {
        String sql="SELECT `followedName` followedName  FROM `relation` WHERE  `followerName`=?";
        return queryForList(Relation.class,sql,username);
    }

    @Override
    public Integer queryFollowedNum(String username) {
        String sql="SELECT count(*) FROM `relation` WHERE  `followerName`=?";

        Number count = (Number) queryForSingleValue(sql,username);
        return count.intValue();
    }

    @Override
    public Integer addFollower(String followerName, String followedName) {
        String sql= "INSERT INTO `relation` (`followerName`,`followedName`) VALUES(?,?)";
        return update(sql,followerName,followedName);
    }

    @Override
    public Integer deleteFollower(String followerName, String followedName) {
        String sql= "DELETE FROM `relation` WHERE `followerName`=? AND `followedName`=?";
        return update(sql,followerName,followedName);
    }

    /**
     *
     * @param followerName
     * @param followedName
     * @return 返回true就说明当前登录用户没有关注目前正在查看的用户，返回false说明已经关注
     */
    @Override
    public boolean isFollower(String followerName, String followedName) {
        String sql= " SELECT * FROM `relation` WHERE `followerName`=? AND `followedName`=?";
        List<Relation> relations = queryForList(Relation.class, sql, followerName, followedName);
        if(relations.size()==0){
            //返回true说明还没关注
            return true;
        }else {
            //返回false说明已经关注
            return false;
        }

    }

    @Override
    public List<User> queryIsFreezedUser() {
        String sql= " SELECT * FROM `user` WHERE `role`=\"user\" AND `isFreezed`=1";
        return queryForList(User.class,sql);
    }

    @Override
    public int setIsFreezed(Integer userId) {
        String sql= " UPDATE `user` SET `isFreezed`=1 WHERE id =?";

        return update(sql,userId);
    }

    @Override
    public int deleteIsFreezed(Integer userId) {
        String sql= "UPDATE `user` SET `isFreezed`=0 WHERE id =?";

        return update(sql,userId);
    }

    @Override
    public int updateUserNameAndPassword(String userName,String originalUserName, String passWord) {

        String sql= "UPDATE `user` SET `username`=? ,`password`=? WHERE `username`=?";
        update(sql,userName,passWord,originalUserName);
        String sql1= "UPDATE `relation` SET `followerName`=? WHERE `followerName`=?";
        update(sql1,userName,originalUserName);

        String sql2= "UPDATE `relation` SET `followedName`=? WHERE `followedName`=?";
        update(sql2,userName,originalUserName);


        String sql3= " UPDATE `comment` SET `username`=? WHERE `username`=?";
        return update(sql3,userName,originalUserName);


    }
}
